package kr.or.kosta.otr.controller.boardcontroller;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kr.or.kosta.otr.service.boardservice.BoardService;
import kr.or.kosta.otr.utill.UploadDataVO;
import kr.or.kosta.otr.vo.BoardVO;
import kr.or.kosta.otr.vo.CommentVO;
import kr.or.kosta.otr.vo.FileVO;
import kr.or.kosta.otr.vo.UserVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BoardController {
	@Resource(name="BoardService")
	BoardService boardService;
	
	@Resource(name="C_fileUploadProperties")
	Properties C_fileUploadProperties;
	
	String path;
	
	@RequestMapping(value = "/writeBoard.action")
	public ModelAndView writeBoard(HttpSession session,
			@ModelAttribute UploadDataVO dvo,
			@RequestParam(value = "board_title") String board_title,
			@RequestParam(value = "board_content") String board_content) throws Exception{
		MultipartFile mfile=dvo.getUploadFile();
		String uploadFile="";
		path=C_fileUploadProperties.getProperty("file.upload.board.path");
		if(mfile.isEmpty()==false){
			File copyFile=new File(path+mfile.getOriginalFilename());
			mfile.transferTo(copyFile);
			uploadFile=mfile.getOriginalFilename();
		}
		
		UserVO uvo=(UserVO) session.getAttribute("userVo");
		BoardVO bvo=new BoardVO(0, uvo.getUserId(), board_title, board_content, null);
		FileVO fvo=new FileVO(0, bvo, uploadFile, "upload_file");
		bvo=boardService.writeBoard(bvo, fvo);

		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("bvo", bvo);
		map.put("uploadFile", uploadFile);
		
		return new ModelAndView("board/board_write_result", "map", map);
	}
	
	@RequestMapping(value = "/updateBoardView.action")
	public ModelAndView updateBoardView(@RequestParam(value = "boardId") String boardId) throws SQLException{
		int bid=Integer.parseInt(boardId);
		HashMap<String, Object> map=boardService.getOneBoardRefresh(bid);
		return new ModelAndView("board/board_update", "map", map);
	}
	
	@RequestMapping(value = "/updateBoard.action")
	public ModelAndView updateBoard(@ModelAttribute UploadDataVO dvo,
			@RequestParam(value = "boardId") String boardId,
			@RequestParam(value = "updateTitle") String updateTitle,
			@RequestParam(value = "updateContent") String updateContent) throws Exception{
		int bid=Integer.parseInt(boardId);
		MultipartFile mfile=dvo.getUploadFile();
		String uploadFile=boardService.getOneFileName(bid);
		path=C_fileUploadProperties.getProperty("file.upload.board.path");
		if(mfile!=null && mfile.isEmpty()==false){
			File copyFile=new File(path+mfile.getOriginalFilename());
			mfile.transferTo(copyFile);
			uploadFile=mfile.getOriginalFilename();
		}
		
		BoardVO bvo=new BoardVO(bid, null, updateTitle, updateContent, null);
		FileVO fvo=new FileVO(0, bvo, uploadFile, "upload_file");
		boardService.updateBoard(bvo, fvo);
		
		HashMap<String, Object> map=boardService.getOneBoardRefresh(bid);
		return new ModelAndView("board/board_update_result", "map", map);
	}

	@RequestMapping(value = "/deleteBoard.action")
	public ModelAndView deleteBoard(@RequestParam(value = "boardId") String boardId) throws SQLException{
		int bid=Integer.parseInt(boardId);
		boardService.deleteBoard(bid);
		return new ModelAndView("redirect:/getBoardListByPage.action?pageNo=1");
	}

	@RequestMapping(value = "/getOneBoardById.action")
	public ModelAndView getOneBoardById(@RequestParam(value = "boardId") String boardId) throws SQLException{
		int bid=Integer.parseInt(boardId);
		HashMap<String, Object> map=boardService.getOneBoardById(bid);
		return new ModelAndView("board/board_content", "map", map);
	}
	
	@RequestMapping(value = "/getOneBoardRefresh.action")
	public ModelAndView getOneBoardRefresh(@RequestParam(value = "boardId") String boardId) throws SQLException{
		int bid=Integer.parseInt(boardId);
		HashMap<String, Object> map=boardService.getOneBoardRefresh(bid);
		return new ModelAndView("board/board_content", "map", map);
	}
	
	@RequestMapping(value = "/getBoardListByPage.action")
	public ModelAndView getBoardListByPage(@RequestParam(value = "pageNo") String pageNo) throws SQLException{
		return new ModelAndView("board/board", "boardList", boardService.getBoardListByPage(pageNo));
	}
	
	@RequestMapping(value = "/getBoardBySearch.action")
	public ModelAndView getBoardBySearch(@RequestParam(value = "searchCommand") String searchCommand,
			@RequestParam(value = "searchValue") String searchValue) throws SQLException{
		ArrayList<BoardVO> list=new ArrayList<BoardVO>();
		if(searchCommand.equals("searchingId")){
			list=boardService.getBoardByUserId(searchValue);
		}else if(searchCommand.equals("searchingTitle")){
			list=boardService.getBoardByTitle(searchValue);
		}else if(searchCommand.equals("searchingContent")){
			list=boardService.getBoardByContent(searchValue);
		}
		return new ModelAndView("board/board_search_result", "list", list);
	}
	
	@RequestMapping(value = "/deleteFile.action")
	public ModelAndView deleteFile(@RequestParam(value = "boardId") String boardId) throws SQLException{
		int bid=Integer.parseInt(boardId);
		boardService.deleteFile(bid);
		return new ModelAndView("JsonView");
	}
	
	@RequestMapping(value = "/downloadFile.action")
	public ModelAndView downloadFile(@RequestParam(value = "fileName") String fileName) throws SQLException{
		HashMap map=  new HashMap();
		map.put("path", path);
		return new ModelAndView("downloadView",map);
	}
	
	@RequestMapping(value = "/writeParentComment.action")
	public ModelAndView writeParentComment(
			@RequestParam(value = "boardId") String boardId,
			@RequestParam(value = "commentUserId") String commentUserId,
			@RequestParam(value = "commentContent") String commentContent) throws SQLException{
		int bid=Integer.parseInt(boardId);
		CommentVO cvo=new CommentVO(0, new BoardVO(bid), commentUserId, commentContent);
		boardService.writeParentComment(cvo);
		return new ModelAndView("redirect:/getOneBoardRefresh.action?boardId="+boardId);
	}
	
	@RequestMapping(value = "/deleteComment.action")
	public ModelAndView deleteComment(
			@RequestParam(value = "commentId") String commentId,
			@RequestParam(value = "boardId") String boardId) throws SQLException{
		int cid=Integer.parseInt(commentId);
		boardService.deleteComment(cid);
		return new ModelAndView("redirect:/getOneBoardRefresh.action?boardId="+boardId);
	}
	
	@RequestMapping(value = "/updateComment.action")
	public ModelAndView updateComment(
			@RequestParam(value = "boardId") String boardId,
			@RequestParam(value = "commentId") String commentId,
			@RequestParam(value = "commentContent") String commentContent) throws SQLException{
		CommentVO cvo=new CommentVO();
		int cid=Integer.parseInt(commentId);
		cvo.setCommentId(cid);
		cvo.setCommentContent(commentContent);
		boardService.updateComment(cvo);
		return new ModelAndView("redirect:/getOneBoardRefresh.action?boardId="+boardId);
	}
	
	@RequestMapping(value = "/writeChildComment.action")
	public ModelAndView writeChildComment(
			@RequestParam(value = "boardId") String boardId,
			@RequestParam(value = "parentCommentId") String parentCommentId,
			@RequestParam(value = "commentUserId") String commentUserId,
			@RequestParam(value = "commentContent") String commentContent) throws SQLException{
		int bid=Integer.parseInt(boardId);
		int pcid=Integer.parseInt(parentCommentId);
		boardService.writeChildComment(pcid, new CommentVO(0, new BoardVO(bid), commentUserId, commentContent));
		return new ModelAndView("redirect:/getOneBoardRefresh.action?boardId="+boardId);
	}
}
